/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package soft.hb.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anthony.rvhelper.adapter.CommonAdapter;
import com.anthony.rvhelper.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import soft.hb.R;
import soft.hb.model.Task;
import soft.hb.util.HttpRequest;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.util.JSON;
import zuo.biao.library.util.Log;

/**
 * 非道路移动器械待办任务
 *
 * @author yuanweiwei
 */
public class PendingTaskActivity extends BaseActivity implements OnClickListener, OnHttpResponseListener {
    public static final String TAG = "PendingTaskActivity";


    private PendingTaskAdapter mAdapter;

    /**
     * 获取启动IndexActivity的intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, PendingTaskActivity.class);
    }


    @Override
    public Activity getActivity() {
        return this; //必须return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pending_task_activity);
        intent = getIntent();

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private RecyclerView recyclerView;


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void initView() {//必须调用
        recyclerView = findView(R.id.rv_task);

    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void initData() {//必须调用
        mAdapter = new PendingTaskAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);

        showProgressDialog(R.string.loading);
        HttpRequest.pendingTaskList(0, new OnHttpResponseListener() {
            @Override
            public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                JSONObject jsonObject = JSON.parseObject(resultJson);
                JSONArray data = jsonObject == null ? null : jsonObject.getJSONArray("data");
                Log.i("TAG", resultJson);
                if (data != null && data.size() > 0) {
                    List<Task> lsTask = new ArrayList<>();
                    for (int i = 0; i < data.size(); i++) {
                        JSONObject jo = data.getJSONObject(i);
                        Task task = JSON.parseObject(jo, Task.class);
                        lsTask.add(task);
                    }
                    onDataReceived(lsTask);
                }
                dismissProgressDialog();
            }
        });
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initEvent() {//必须调用
    }


    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:


                break;


        }
    }


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

    }


    @Override
    public void finish() {
        super.finish();

    }

    @Override
    public void onHttpResponse(int requestCode, String resultJson, Exception e) {

    }


    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    /**
     * todo load topic images
     *
     * @param "List<TeacherBean>"
     */
    private void onDataReceived(List<Task> taskList) {
        mAdapter.addDataAll(taskList);
        mAdapter.notifyDataSetChanged();

    }


    public class PendingTaskAdapter extends CommonAdapter<Task> {
        public PendingTaskAdapter(Context context) {
            super(context, R.layout.pending_task_view);
        }


        @Override
        protected void convert(ViewHolder holder, final Task item, int position) {
            holder.setText(R.id.tv_subject, item.getSubject());
            holder.setText(R.id.tv_project_name, item.getProject().getName());
            holder.setText(R.id.tv_user_name, item.getUser().getName());
            holder.setText(R.id.tv_etime, item.getEtime());
            holder.setOnClickListener(R.id.layout_click, new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, ProjectDetailsActivity.class);
                    intent.putExtra("projectId", item.getProject().getId());
                    intent.putExtra("taskId", item.getId());
                    startActivity(intent);

                }
            });

        }
    }
}